﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Salon
{
    public partial class RegisterCars : Form
    {
        public Car ChosenCar { get; set; }
        public int Mode; // Tryb pracy formularza
        
        public RegisterCars()
        {
            Mode = 0;
            InitializeComponent();

            dgvCarGrid.AutoGenerateColumns = false;
            Text = Forms.CAR_REGISTER;

            btnChoose.Enabled = btnChoose.Visible = false;
        }

        public RegisterCars(bool dict)
        {
            Mode = 0;
            InitializeComponent();
            dgvCarGrid.AutoGenerateColumns = false;
            Text = Forms.CAR_REGISTER;
            if (dict)
            {
                btnAdd.Enabled = btnChoose.Visible = false;
                btnEdit.Enabled = btnChoose.Visible = false;
                btnDelete.Enabled = btnChoose.Visible = false;
                btnChoose.Enabled = btnChoose.Visible = true;
                btnChoose.Top = btnAdd.Top;
                Mode = 1; // Wybieranie samochodu
            }
            else 
            {
                btnAdd.Enabled = true;
                btnEdit.Enabled = true;
                btnDelete.Enabled = true;
                btnChoose.Enabled = btnChoose.Visible = false;
            }
        }

        private void RegisterCars_Load(object sender, EventArgs e)
        {
            cmbMark.Initialize();
            cmbModel.Initialize();
            cmbSalon.Initialize();
            if ( Mode == 1 )
            {
                cmbSalon.SelectedValue = Global.currentUser.Salon.Id;
                cmbSalon.Enabled = false;
            }
            
            // UPRAWNIENIA - pierwsze podejście

            if( (Global.currentUser.Role & (int)(Roles.Administrator | Roles.SuperAdministrator)) == 0 )
                btnDelete.Enabled = false; // usówanie

            if ((Global.currentUser.Role & (int)(Roles.Administrator | Roles.SuperAdministrator | Roles.Manager)) == 0)
                btnEdit.Enabled = btnAdd.Enabled = false; // edycja i dodawanie
            

            FillTable();
        }


        private void FillTable()
        {
            BindingSource bs = new BindingSource();
            CarService us = new CarService();
            IList<IFilter> fl = Global.getFilterList(this); // filtry z formatki
            fl.Add(new Filter("sold", 0)); // nie sprzedane
            fl.Add(new Filter("active", 1)); // aktywne

            IList<Car> Cars = us.getCars( fl );

            foreach (Car Car in Cars)
                bs.Add(Car);

            dgvCarGrid.DataSource = bs;
        }


        private void dgvCarGrid_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            // UPRAWNIENIA
            if ((Global.currentUser.Role & (int)(Roles.Administrator | Roles.SuperAdministrator)) == 0)
                return;
            
            if( Mode == 1 )
            {
                // jeśli wybieranie - podwójne klikmnięcie wybiera a nie edytuje
                this.ChosenCar = (Car)dgvCarGrid.Rows[e.RowIndex].DataBoundItem;
                this.Close();
            }
            else
            if (!Global.MainPage.checkIfFormOpen(Forms.CAR_FORM))
            {
                FormCar fc = new FormCar((Car)dgvCarGrid.Rows[e.RowIndex].DataBoundItem);
                fc.MdiParent = Global.MainPage;
                fc.Text = Forms.CAR_FORM;
                fc.Show();
            }
        }

        private void btnFilter_Click(object sender, EventArgs e)
        {
            FillTable();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            if (Mode == 1) // w trybie wyboru samochodu nie można zmienić salonu
            {
                foreach (IFilter filtr in Global.getFilterList(this))
                {
                    if(filtr.ColumnName != "car.salon") filtr.CleanUp();
                }
            }
            else
            {
                foreach (IFilter filtr in Global.getFilterList(this))
                {
                    filtr.CleanUp();
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Global.MainPage.openNewCarForm(null);
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvCarGrid.SelectedRows.Count != 1)
            {
                MessageBox.Show("Proszę zaznaczyć dokładnie jeden wiersz do edycji.");
                return;
            }

            Global.MainPage.openNewCarForm((Car)dgvCarGrid.SelectedRows[0].DataBoundItem);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dgvCarGrid.SelectedRows.Count < 1)
            {
                MessageBox.Show("Proszę zaznaczyć przynajmniej jeden wiersz do usunięcia.");
                return;
            }

            if (MessageBox.Show("Czy na pewno chcesz usunąć wszystkich zaznaczonych klientów?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                CarService us = new CarService();
                for (int i = 0; i < dgvCarGrid.SelectedRows.Count; i++)
                    us.deleteCar((Car)dgvCarGrid.SelectedRows[i].DataBoundItem);
                FillTable();
            }
        }


        private void btnChoose_Click(object sender, EventArgs e)
        {
            if (dgvCarGrid.SelectedRows.Count < 1)
            {
                MessageBox.Show("Proszę wybrać dokładnie jeden samochód");

            }
            else {
                this.ChosenCar = (Car)dgvCarGrid.SelectedRows[0].DataBoundItem;
                this.Close();
            }

        }

        private void cmbMark_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbModel.SelectedItem = null;
        }


    }
}
